package com.ruoyi.waterDistribution.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.business.waterDistribution.WiFarmplanM;
import com.ruoyi.waterDistribution.service.IWiFarmplanMService;
import com.ruoyi.waterDistribution.vo.WiLandBVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 月农户限额水量及计划水量计算Controller
 *
 * @author ps
 * @date 2023-02-16
 */
@RestController
@RequestMapping("/WiFarmplanM")
public class WiFarmplanMController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(WiFarmplanMController.class);

    @Autowired
    private IWiFarmplanMService wiFarmplanMService;

    /**
     * 导入上一年计划
     */
    @GetMapping("/importLastYearPlan")
    public AjaxResult importLastYearPlan(String year, String orgCd) {
        log.info("导入上一年数据 入参 : " + year + "=" + orgCd);
        List<WiFarmplanM> wiFarmplanMList = wiFarmplanMService.importLastYearPlan(year, orgCd);
        return AjaxResult.success(wiFarmplanMList);
    }

    /**
     * 汇总农户当年各月旬计划
     */
    @GetMapping("/calcTenPlan")
    public AjaxResult getTenPlan(String year, String farmCd) throws Exception {
        log.info("calcTenPlan 入参 : " + year + "==" + farmCd);
        return toAjax(wiFarmplanMService.setTenPlan(year, farmCd));
    }

    /**
     * 根据机构编码获取该机构下属所有地块
     *
     * @param farmCd 农户代码
     * @return
     */
    @GetMapping("/getWiLandBListByFarmCd")
    public TableDataInfo getWiLandBListByFarmCd(String year, String farmCd) {
        log.info("getWiLandBListByOrgCd入参 : " + farmCd);
        List<WiLandBVo> list = wiFarmplanMService.getWiLandBListByFarmCd(year, farmCd);
        return getDataTable(list);
    }

    /**
     * 查询月农户限额水量及计划水量计算列表
     */
    @GetMapping("/list")
    public TableDataInfo list(WiFarmplanM wiFarmplanM) {
        startPage();
        List<WiFarmplanM> list = wiFarmplanMService.selectWiFarmplanMList(wiFarmplanM);
        return getDataTable(list);
    }

    /**
     * 导出月农户限额水量及计划水量计算列表
     */

    @Log(title = "月农户限额水量及计划水量计算", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WiFarmplanM wiFarmplanM) {
        List<WiFarmplanM> list = wiFarmplanMService.selectWiFarmplanMList(wiFarmplanM);
        ExcelUtil<WiFarmplanM> util = new ExcelUtil<WiFarmplanM>(WiFarmplanM.class);
        util.exportExcel(response, list, "月农户限额水量及计划水量计算数据");
    }

    /**
     * 获取月农户限额水量及计划水量计算详细信息
     */

    @GetMapping(value = "/{farmCd}")
    public AjaxResult getInfo(@PathVariable("farmCd") String farmCd) {
        return success(wiFarmplanMService.selectWiFarmplanMByFarmCd(farmCd));
    }

    /**
     * 新增月农户限额水量及计划水量计算
     */

    @Log(title = "月农户限额水量及计划水量计算", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WiFarmplanM wiFarmplanM) {
        return toAjax(wiFarmplanMService.insertWiFarmplanM(wiFarmplanM));
    }

    /**
     * 修改月农户限额水量及计划水量计算
     */

    @Log(title = "月农户限额水量及计划水量计算", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WiFarmplanM wiFarmplanM) {
        return toAjax(wiFarmplanMService.updateWiFarmplanM(wiFarmplanM));
    }

    /**
     * 删除月农户限额水量及计划水量计算
     */

    @Log(title = "月农户限额水量及计划水量计算", businessType = BusinessType.DELETE)
    @DeleteMapping("/{farmCds}")
    public AjaxResult remove(@PathVariable String[] farmCds) {
        return toAjax(wiFarmplanMService.deleteWiFarmplanMByFarmCds(farmCds));
    }
}
